<?php

namespace App\Filament\Resources\Admin\Roles\Tables;

use App\Filament\Resources\Admin\Roles\RoleResource;
use Filament\Actions\Action;
use Filament\Actions\CreateAction;
use Filament\Actions\DeleteAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\TextInput;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Spatie\Permission\Models\Role;

class AdminRolesTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->striped()
            ->modifyQueryUsing(fn (Builder $query) => $query->with('permissions', 'permissions.parent'))
            ->columns([
                TextColumn::make('id')->sortable(),
                TextColumn::make('title')
                    ->label('角色名称'),
                TextColumn::make('name')
                    ->label('角色标识'),
                TextColumn::make('permissions')
                    ->label('权限标识')
                    ->formatStateUsing(function ($state) {
                        if ($state->parent) {
                            return $state->parent->title.'-'.$state->title;
                        }

                        return $state->title;
                    })
                    ->bulleted()
                    ->limitList()
                    ->expandableLimitedList()
                    ->listWithLineBreaks(),
                TextColumn::make('created_at')
                    ->label('创建时间')
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                //
            ])
            ->recordActions([
                Action::make('permission')
                    ->icon('heroicon-o-cube')
                    ->label('分配权限')
                    ->visible(fn () => auth()->user()->can('admin.role.permission'))
                    ->url(fn ($record): string => RoleResource::getUrl('permission', ['id' => $record->id]))
                    ->openUrlInNewTab(false),
                EditAction::make()
                    ->modal()
                    ->schema([
                        TextInput::make('title')
                            ->label('角色名称')
                            ->placeholder('请输入角色名称')
                            ->required()
                            ->maxLength(255),
                        TextInput::make('name')
                            ->label('角色标识')
                            ->placeholder('请输入角色标识')
                            ->required()
                            ->maxLength(255)
                            ->unique(Role::class),
                    ])
                    ->visible(fn () => auth()->user()->can('admin.role.edit'))
                    ->action(function (array $data, $record): void {
                        $record->update($data);
                    })
                    ->successNotificationTitle('角色更新成功'),
                DeleteAction::make('delete')
                    ->label('删除')
                    ->requiresConfirmation()
                    ->action(function ($arguments, $record): void {
                        $record->delete();
                        $record->users()->detach();
                    })
                    ->visible(fn () => auth()->user()->can('admin.role.delete'))
                    ->successNotificationTitle('角色删除成功')
                    ->modalHeading('删除角色')
                    ->modalDescription('确定要删除该角色吗？'),
            ])
            ->recordAction(null)
            ->toolbarActions([
                CreateAction::make()
                    ->modal()
                    ->schema([
                        TextInput::make('title')
                            ->label('角色名称')
                            ->placeholder('请输入角色名称')
                            ->required()
                            ->maxLength(255),
                        TextInput::make('name')
                            ->label('角色标识')
                            ->placeholder('请输入角色标识')
                            ->required()
                            ->maxLength(255)
                            ->unique(Role::class),
                    ])
                    ->action(function (array $data): void {
                        $data['guard_name'] = 'admin';
                        Role::create($data);
                    })
                    ->visible(fn () => auth()->user()->can('admin.role.add'))
                    ->createAnother(false)
                    ->successNotificationTitle('角色创建成功'),
            ])
            ->deferColumnManager(false);
    }
}
